package org.feng.utils.condition;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.feng.utils.HttpUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author by feng
 * @des 条件构造器
 * @date 2021/4/27 14:03
 */
public class Condition {
    /**
     * 分页构造器
     *
     * @param <T>
     * @return
     */
    public static <T> IPage<T> getPage() {
        HttpServletRequest request = HttpUtils.getRequest();
        String current = request.getParameter("current");
        String size = request.getParameter("size");
        return new Page<>(Long.parseLong(current), Long.parseLong(size));
    }

    /**
     * =条件构造器，默认创建时间 倒叙排序
     *
     * @param entity
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> getQueryWrapper(T entity) {
        return new QueryWrapper<>(entity);
    }

    /**
     * =条件构造器，默认创建时间 倒叙排序
     *
     * @param entity
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> getQueryWrapper(T entity, String rule, String... column) {
        if (rule.equalsIgnoreCase("desc")) {
            return Wrappers.<T>query(entity).orderByDesc(column);
        } else {
            return Wrappers.<T>query(entity).orderByAsc(column);
        }
    }

}
